package edu.neumont.pro180._2009_q2;

import java.awt.Point;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import edu.neumont.pro180._2009_q2.interfaces.IGamePiece;


/**
 * A Checker piece.
 * @author Tyler Young
 */
public class Checker implements IGamePiece {
	private Square position;

	/**
	 * Gets the Square that the Checker is on.
	 * @return The Square that the piece is currently located on.
	 */
	public Square getSquare() {
		return this.position;
	}
	
	public int getValue() {
		return 0;
	}

	/**
	 * Moves the piece to the Square indicated.
	 * @param position The Square to move the piece to.
	 */
	public boolean setPosition(Square position) {
		if (this.position != position && position != null) {
			if (this.position != null) {
				// Remove the checker from its current square.
				this.position.setGamePiece(null).execute();
			}
			// Add the checker to its new square.
			position.setGamePiece(this).execute();
		}
		this.position = position;
		return this.position != null;
	}

	/**
	 * Get the set of Squares that the piece can move to.
	 * @return The set of Squares that the piece can move to.
	 */
	public Collection<Square> getPossibleSquares() {
		Set<Square> possibleMoves = new HashSet<Square>();
		Collection<Square> diagonals = getDiagonals(this.position);

		for (Square diagonal : diagonals) {
			if (diagonal.hasGamePiece()) {
				// See if there's a blank square on the other side to jump to.
				// NOTE: Could make this a recursive function to do multiple
				// jumps.
				Point offset = this.position.getOffset(diagonal);
				Square jumpDestination = diagonal.getNeighborOrSelf(offset);
				if ((diagonal != jumpDestination) && !jumpDestination.hasGamePiece()) {
					possibleMoves.add(jumpDestination);
				}
			} else {
				possibleMoves.add(diagonal);
			}
		}

		return possibleMoves;
	}

	/**
	 * Returns the diagonal Squares surrounding the piece. It's a private function to help with checker movement.
	 * @param startingSquare The Square to start at.
	 * @return The set of Squares diagonally around the startingSquare.
	 */
	private Collection<Square> getDiagonals(Square startingSquare) {
		Set<Square> possibleMoves = new HashSet<Square>();
		for (int x = -1; x < 2; x += 2) {
			for (int y = -1; y < 2; y += 2) {
				Square diagonalNeighbor = startingSquare.getNeighbor(new Point(x, y));
				if (null != diagonalNeighbor) {
					possibleMoves.add(diagonalNeighbor);
				}
			}
		}
		return possibleMoves;
	}

	@Override
	public Player getPlayer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GamePieceMove getMove(Square destination) {
		// TODO Auto-generated method stub
		return null;
	}
}
